In [1]:
import plotly.plotly
from plotly.graph_objs import Scatter, Layout
from numpy import arange, cos
import numpy as np
import math
import sympy as sp
x, y, z = sp.symbols('x y z')
sp.init_printing()
#sp.init_printing(use_unicode=True)
#sp.init_printing(use_latex='mathjax')
from sympy.utilities.lambdify import lambdify, implemented_function
print('imports completed')
In [2]:
class MyFunctions:
# class init fn
def __init__(self, low_bound, high_bound, samples):
self.low_bound = low_bound
self.high_bound = high_bound
self.samples = samples
def get_space(self):
return np.linspace(self.low_bound, self.high_bound, self.samples)
# functions for homework
def h(self, x):
expr = 2*x ** 3 + 6*x ** 2 - 12
return expr
def g(self, x):
expr = 13*x ** 2 + 76*x
expr_prime = sp.diff(expr)
result = expr_prime.evalf(subs={x:100})
return expr_prime
def eval_sympy_fn(self, expr_str, x_val):
#sympy_expr = sp.parsing.parse_expr(expr_str)
sympy_expr = expr_str
print(sympy_expr)
result = sympy_expr.evalf(subs={x:x_val})
return result
# Test MyFunctions
mf = MyFunctions(-10, 10, 20)
# build data array test function
def build_data(fn_name, space):
tmparray = []
fn = getattr(mf, fn_name)
for i in space:
tmparray.append(float(fn(i)))
#print(tmparray)
return np.array(tmparray)
# build sympy data array using lambda
def test_impl_fn(space):
f2 = implemented_function(sp.Function('f2'), lambda x: 2*x ** 3 + 6*x ** 2 - 12)
lam_f2 = lambdify(x, f2(x), 'numpy')
#print(lam_f2(a))
return lam_f2(space)
def lambdify_fun(fn, space):
lam = lambdify(x, fn, 'numpy')
return lam(space)
def lambdify_prime_fun(fn, space):
expr_prime = sp.diff(fn)
lam_p = lambdify(x, expr_prime, 'numpy')
return lam_p(space)
def lambdify_doubleprime_fun(fn, space):
expr_primeprime = sp.diff(sp.diff(fn))
lam_dp = lambdify(x, expr_primeprime, 'numpy')
return lam_dp(space)
# TESTS
#f1 = implemented_function(sp.Function('f1'), lambda x: x+1)
#lam_f1 = lambdify(x, f1(x))
#r = lam_f1(10)
#print(r)
# THIS ALL WORKS
#a = np.arange(10)
#print(a)
#exstr = lambdastr(x, 2*x ** 3 + 6*x ** 2 - 12)
#f2 = implemented_function(sp.Function('f2'), lambda x: 2*x ** 3 + 6*x ** 2 - 12)
#lam_f2 = lambdify(x, f2(x), 'numpy')
#print('soln is ')
#print(lam_f2(a))
# New Test
#a = np.arange(10)
#print(a)
#print(test_h_fn(a))
#print(test_g_fn(a))
In [3]:
# Instantiate
mf = MyFunctions(-50, 50, 100)
#expression = sp.sympify('2*x ** 3 + 6*x ** 2 - 12')
#la = sp.latex('( (x - 1) 2 / 3)')
expression = sp.sympify(sp.Rational(1/60)*x**3 + 5*x +10)
print(expression)
# Required for displaying plotly in jupyter notebook
plotly.offline.init_notebook_mode(connected=True)
# Create traces
#trace1 = Scatter(x=mf.get_space(), y=build_data('h', mf.get_space()), name='y', line=dict(color='#bc42f4'))
#trace2 = Scatter(x=mf.get_space(), y=build_data('g', mf.get_space()), name='y prime', line=dict(color='#f44641'))
#trace1 = Scatter(x=the_space, y=test_h_fn(the_space), name='y', line=dict(color='#bc42f4'))
the_space = mf.get_space()
trace1 = Scatter(x=the_space, y=lambdify_fun(expression, the_space), name='y', line=dict(color='#bc42f4'))
trace2 = Scatter(x=the_space, y=lambdify_prime_fun(expression, the_space), name='y prime', line=dict(color='#f44641'))
trace3 = Scatter(x=the_space, y=lambdify_doubleprime_fun(expression, the_space), name='y double prime', line=dict(color='#0A8A0A'))
# plot it
plotly.offline.iplot({
"data": [trace1, trace2, trace3],
"layout": Layout(title="HW Test1")
})
In [ ]: